"""Build tests"""
import daos_build
import os

def scons():
    """Execute build"""
    Import('base_env', 'prereqs')

    if not prereqs.test_requested():
        return

    libs_client = ['daos', 'daos_common', 'gurt', 'm', 'cart', 'uuid', 'cmocka', 'daos_tests']
    libs_server = ['daos', 'daos_common_pmem', 'gurt', 'm', 'cart', 'uuid', 'cmocka', 'daos_tests']

    denv = base_env.Clone()

    if not GetOption('help') and not GetOption('clean'):
        mpi = daos_build.configure_mpi(denv, libs_client)
        if mpi is None:
            print("\nSkipping compilation for tests that need MPI")
            print("Install and load mpich or openmpi\n")
            return

    # Add runtime paths for daos libraries
    denv.AppendUnique(RPATH_FULL=['$PREFIX/lib64/daos_srv'])

    denv.Append(CPPPATH=[Dir('suite').srcnode()])
    denv.AppendUnique(CPPPATH=[Dir('../mgmt').srcnode()])
    prereqs.require(denv, 'argobots', 'hwloc', 'protobufc', 'pmdk', 'isal')

    daos_build.program(denv, 'simple_array', 'simple_array.c', LIBS=libs_client)
    daos_build.program(denv, 'simple_obj', 'simple_obj.c', LIBS=libs_client)
    libs_client += ['pthread', 'dts']
    libs_server += ['pthread', 'dts']

    daos_racer = daos_build.program(denv, 'daos_racer',
                                    ['daos_racer.c'],
                                    LIBS=libs_client)
    denv.Install('$PREFIX/bin/', daos_racer)

    perf_common = denv.StaticObject(['perf_common.c'])

    daos_perf = daos_build.program(denv, 'daos_perf',
                                   ['daos_perf.c', perf_common],
                                   LIBS=libs_client)
    denv.Install('$PREFIX/bin/', daos_perf)

    libs_server += ['vos', 'bio', 'abt']
    vos_engine = denv.StaticObject(['vos_engine.c'])

    vos_perf = daos_build.program(denv, 'vos_perf',
                                  ['vos_perf.c', perf_common, vos_engine],
                                  LIBS=libs_server)
    denv.Install('$PREFIX/bin/', vos_perf)

    Import('mpi_cmd_parser')
    obj_ctl = daos_build.program(denv, 'obj_ctl',
                                 ['obj_ctl.c', mpi_cmd_parser, vos_engine],
                                 LIBS=libs_server)
    denv.Install('$PREFIX/bin/', obj_ctl)

    jobtest = daos_build.program(denv, 'jobtest', ['jobtest.c'], LIBS=libs_client)
    denv.Install('$PREFIX/bin/', jobtest)

    # tests
    SConscript('suite/SConscript', exports=['denv'])

    # Build drpc_test
    SConscript('drpc/SConscript')

    # Build security_test
    SConscript('security/SConscript')

    # ftest
    SConscript('ftest/SConscript')

if __name__ == "SCons.Script":
    scons()
